﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[NetworkSettings(channel=0, sendInterval=0f)]
public class Interact : CharacterBehaviour
{
    [CompilerGenerated]
    private static Action<IInteractable> <>f__am$cache3;
    private Collider[] enteredColliders = new Collider[30];
    public float triggerRadius = 1f;

    public event Action<IInteractable> onTargetEnter;

    public Interact()
    {
        if (<>f__am$cache3 == null)
        {
            <>f__am$cache3 = new Action<IInteractable>(Interact.<onTargetEnter>m__50);
        }
        this.onTargetEnter = <>f__am$cache3;
    }

    [CompilerGenerated]
    private static void <onTargetEnter>m__50(IInteractable c)
    {
    }

    public IInteractable DoInteract()
    {
        IInteractable enteredTarget = this.GetEnteredTarget();
        if (enteredTarget == null)
        {
            return null;
        }
        enteredTarget.Interact(base.self);
        return enteredTarget;
    }

    public IInteractable GetEnteredTarget()
    {
        Collider collider;
        return this.GetEnteredTarget(out collider);
    }

    public IInteractable GetEnteredTarget(out Collider targetCollider)
    {
        int num = Physics.OverlapSphereNonAlloc(base.transform.position, this.triggerRadius, this.enteredColliders);
        float positiveInfinity = float.PositiveInfinity;
        IInteractable interactable = null;
        Collider collider = null;
        for (int i = 0; i < num; i++)
        {
            Collider comp = this.enteredColliders[i];
            IInteractable componentInRoot = comp.GetComponentInRoot(typeof(IInteractable)) as IInteractable;
            if (((componentInRoot != null) && !this.IsNewlyDroppedItem(componentInRoot)) && (((componentInRoot.target == null) || (componentInRoot.target == base.self)) && componentInRoot.CanInteract(base.self)))
            {
                Vector3 vector = comp.transform.position - base.transform.position;
                float sqrMagnitude = vector.sqrMagnitude;
                if (sqrMagnitude < positiveInfinity)
                {
                    positiveInfinity = sqrMagnitude;
                    collider = comp;
                    interactable = componentInRoot;
                }
            }
        }
        targetCollider = collider;
        return interactable;
    }

    public override float GetNetworkSendInterval()
    {
        return 0f;
    }

    private bool IsInvalidTarget(IInteractable target)
    {
        MonoBehaviour behaviour = target as MonoBehaviour;
        return (((behaviour == null) || (behaviour.gameObject == null)) || (behaviour.transform.parent == base.transform));
    }

    private bool IsNewlyDroppedItem(IInteractable target)
    {
        Item item = target as Item;
        if (item == null)
        {
            return false;
        }
        return ((Time.time - item.lastDropTime) < 0.2f);
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    private void UNetVersion()
    {
    }
}

